<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
    <script type="text/javascript" src="../../script/jquery.min.js"></script>
    <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
    <script type="text/javascript" src="../../script/ao_module.js"></script>
    <title>Storage Pool Editor</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <style>
        body{
            background-color:white;
        }
        .themebackground{
            background-color:#242424 !important;
            color:white !important;
            background-image: url("img/slate.png") !important;
            background-repeat: no-repeat !important;
            background-attachment: fixed !important;
            height:100px;
            border: 0px solid transparent !important;
            padding:24px !important;
        }
        .fshList{
            max-height: 300px;
            overflow-y: auto;
        }

        .controls{
            position: absolute;
            top: 12px;
            right: 12px;
        }

        a{
            cursor: pointer;
        }
        .false{
            color: #eb0909;
        }

        .true{
            color: #05b074;
        }

        .backuponly{
            display:none;
        }

        .backgroundIcon{
            position: fixed;
            bottom: 0px;
            right: 0px;
            opacity: 0.1;
            margin-right: -3em;
            margin-bottom: -5em;
            z-index: -99;
            pointer-events: none;
            user-select: none;
        }

    </style>
</head>
<body>
    <div class="backgroundIcon">
        <img class="ui medium image" src="../../img/system/drive-virtual.svg">
    </div>
    <!-- 
    <div class="ui fluid attached segment themebackground" >
        <h4 class="ui inverted header">
            <i class="folder icon"></i>
            <div class="content">
                <span id="pagetitle">Edit File System Handler</span>
            <div class="sub header" id="pageSubTitle">Edit the selected File System Handler (FSH)</div>
            </div>
        </h4>
    </div>
    -->
    <br>
    <div class="ui container">
        <h3 class="ui header">
            <img src="../../img/system/drive-virtual.svg">
            <div class="content">
                <span>Edit File System Handler </span><span id="poolid"></span>
                <div class="sub header">Mount Local or Connect Remote Disk as Virtual Disk on this Host</div>
            </div>
        </h3>
        <form id="mainForm" class="ui form" onsubmit="handleFormSubmit(event);">
            <div class="field" style="display: none;">
                <label>Group</label>
                <input type="text" name="group" id="groupfield" readonly="true">
                </div>
            <div class="field">
                <label>Name</label>
                <input type="text" name="name" placeholder="e.g. My Drive">
            </div>
            <div class="field">
                <label>UUID</label>
                <input type="text" name="uuid" placeholder="e.g. mydrive">
            </div>
            <div class="field">
                <label>Path</label>
                <input type="text" name="path" placeholder="e.g. /media/mydrive" onchange="checkPathProtocol(this);">
            </div>
            <div class="field">
                <label>Access Permission</label>
                <div id="accessfield" class="ui selection dropdown">
                <input type="hidden" name="access" value="readwrite">
                <i class="dropdown icon"></i>
                <div class="default text">Access Permission</div>
                <div class="menu">
                    <div class="item" data-value="readonly">Read Only</div>
                    <div class="item" data-value="readwrite">Read Write</div>
                </div>
                </div>
            </div>
            <div class="field">
                <label>Storage Hierarchy</label>
                <div id="hierarchyfield" class="ui selection dropdown" onchange="handleHierarchyChange(this);">
                <input type="hidden" name="hierarchy" value="public">
                <i class="dropdown icon"></i>
                <div class="default text">Storage Hierarchy</div>
                <div class="menu">
                    <div class="item" data-value="user">Isolated User Folders</div>
                    <div class="item" data-value="public">Public Access Folders</div>
                </div>
                </div>
            </div>
            <div class="ui divider"></div>
            <p>Physical Disks Settings</p>
            <div class="field">
                <label>Filesystem Type</label>
                <div id="fstype" class="ui selection dropdown">
                    <input type="hidden" name="filesystem" value="ntfs" onchange="handleFileSystemTypeChange(this.value);">
                    <i class="dropdown icon"></i>
                    <div class="default text">Filesystem Type</div>
                    <div class="menu">
                        <div class="item" data-value="ext4">EXT4</div>
                        <!-- <div class="item" data-value="ext3">EXT3</div> -->
                        <div class="item" data-value="ntfs">NTFS</div>
                        <div class="item" data-value="vfat">VFAT</div>
                        <div class="item" data-value="fat">FAT</div>
                        <div class="item" data-value="webdav">WebDAV</div>
                        <div class="item" data-value="smb">SMB</div>
                        <div class="item" data-value="ftp">FTP</div>
                        <div class="item" data-value="sftp">SFTP</div>
                    </div>
                </div>
            </div>
            <div class="localfs">
                <div class="field">
                    <label>Mount Device</label>
                    <input type="text" name="mountdev" placeholder="e.g. /dev/sda1">
                </div>
                <div class="field">
                    <label>Mount Point</label>
                    <input type="text" name="mountpt" placeholder="e.g. /media/myfolder">
                </div>
                <div class="field">
                    <div class="ui checkbox">
                    <input type="checkbox" id="automount" tabindex="0" class="hidden">
                    <label>Automount</label>
                    </div>
                </div>
                <br>
            </div>
            <div class="networkfs" style="display:none;">
                <div class="ui divider"></div>
                <p>Security and Authentication</p>
                <div class="field">
                    <label>Username</label>
                    <input type="text" name="username" placeholder="">
                </div>
                <div class="field">
                    <label>Password</label>
                    <input type="password" name="password" placeholder="">
                </div>
                <small>Leave Username / Password field empty for using the old config</small>
                <br><br>
            </div>
            <button class="ui right floated button" onclick='handleCancel();'>Cancel</button>
            <button class="ui green right floated button" type="submit">Confirm</button>
            <br><br><br><br>
        </form>
    </div>
    <script>
        //Get target fsh uuid and group from hash
        var targetFSH = "";
        var opr = "set";
        $(".ui.dropdown").dropdown();
        $(".ui.checkbox").checkbox();

        $(document).ready(function(){
            initEditor();
        });

        function initEditor(){
            if (window.location.hash.length > 0){
                //Get a list of vroot from system
                $("#backupIdList").html(``);
                $.get("../../system/storage/pool/list", function(data){
                    data.forEach(usergroup => {
                        if (usergroup.Storages != null){
                            usergroup.Storages.forEach(storage => {
                                $("#backupIdList").append(`<div class="item" data-value="${storage.UUID}">${storage.Name} (${storage.UUID}:/)</div>`);
                            });
                        }
                    });
                    $("#backupIdList").parent().dropdown();
                    renderFSHCurrentSettings();
                });  
            }
        }

        function handleFormSubmit(e){
            e.preventDefault();
            //Get the form value
            let payload = new FormData(e.target);
            let fshObject = {};
            [...payload.entries()].forEach(function(field){
                fshObject[field[0]] = field[1];
            });

            //Inject other payloads
            fshObject.automount = $("#automount")[0].checked;
            $.ajax({
                url: "../../system/storage/pool/edit",
                method: "POST",
                data: {
                    opr: opr,
                    group: $("#groupfield").val(),
                    config: JSON.stringify(fshObject),
                },
                success: function(data){
                    if (data.error !== undefined){
                        alert(data.error);
                    }else{
                        //Done
                        window.location.href = "updateComplete.html#" + targetFSH;
                    }
                }
            });
        }

        function renderFSHCurrentSettings(){
            //Request server side to provide info on this FSH
            var input = JSON.parse(decodeURIComponent(window.location.hash.substr(1)));
            $("#groupfield").val(input.group);
            targetFSH = input.group;
            if (input.uuid == undefined){
                 //New fsh
                $("#pagetitle").text("New File System Handler");
                $("#pageSubTitle").text("Mount a new file system into this host as storage");
                opr = "new";
            }else{
                $.ajax({
                    url: "../../system/storage/pool/edit",
                    method: "GET",
                    data: {opr: "get", uuid: input.uuid, group: input.group},
                    success: function(data){
                        renderOptionsToForm(data);
                    }
                });

                $("#mainForm").attr("action", "../../system/storage/pool/edit?opr=set&uuid=" + input.uuid + "&group=" + input.group);
            }
        }

        function checkPathProtocol(object){
            var newPath = $(object).val();
            if (newPath.startsWith("http://") || newPath.startsWith("https://")){
                //WebDAV
                $("#fstype").dropdown("set selected", "webdav");
                //newPath = newPath.replace("http://", "");
                //newPath = newPath.replace("https://", "");
                $(object).val(newPath);
            }else if (newPath.startsWith("ftp://")){
                //FTP
                $("#fstype").dropdown("set selected", "ftp");
                newPath = newPath.replace("ftp://", "");
                $(object).val(newPath);
            }else if (newPath.startsWith("sftp://")){
                //SFTP
                $("#fstype").dropdown("set selected", "sftp");
                newPath = newPath.replace("sftp://", "");
                $(object).val(newPath);
            }else if (newPath.startsWith("\\\\")){
                //SMB, Copy directly from Windows
                $("#fstype").dropdown("set selected", "smb");
                newPath = newPath.replace("\\\\", "");
                newPath = newPath.split("\\").join("/");
                $(object).val(newPath);
            }
        }

        function handleHierarchyChange(object){
            var newHierarcy = $(object).find("input").val();
            //Force access mode to readonly if backup mode
            if (newHierarcy == "backup"){
                $("#accessfield").dropdown("set selected", "readonly")
                $("#accessfield").addClass("disabled");
            }else{
                $("#accessfield").removeClass("disabled");
            }
        }

        function handleFileSystemTypeChange(fstype){
            if (isNetworkFs(fstype)){
                $(".localfs").hide();
                $(".networkfs").show();
            }else{
                $(".localfs").show();
                $(".networkfs").hide();
            }
        }

        function isNetworkFs(name){
            name = name.trim();
            if (name == "webdav" || name == "smb" || name == "ftp" || name == "sftp"){
                return true;
            }
            return false;
        }

        function renderOptionsToForm(option){
            console.log(option);
            $("input[name=name]").val(option.name);
            $("input[name=uuid]").val(option.uuid);
            $("input[name=path]").val(option.path);
            $("#accessfield").dropdown("set selected", option.access);
            $("#hierarchyfield").dropdown("set selected", option.hierarchy);
            if (isNetworkFs(option.filesystem)){
                $(".localfs").hide();
                $(".networkfs").show();
            }
            $("#fstype").dropdown("set selected",option.filesystem);
            handleFileSystemTypeChange(option.filesystem);
            $("input[name=mountdev]").val(option.mountdev);
            $("input[name=mountpt]").val(option.mountpt);
            if (option.automount == true){
                //$("input[name=automount]")[0].checked = true;
                $("#automount").parent().checkbox("set checked");
            }
        }

        function handleCancel(){
            ao_module_parentCallback(true);
            ao_module_close();
        }
    </script>
</body>
</html>